﻿package mortal.game.view.playerSystem
{
    import Message.Game.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.fashion.*;
    import mortal.game.view.mount.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.palyer.util.*;
    import mortal.game.view.palyer.view.*;
    import mortal.game.view.petNew.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PlayerSystemController extends Controller
    {
        private var _playerModule:PlayerSystemModule;
        private var _currentContoller:PlayerBaseController;
        private var _dataProvider:DataProvider;
        public var player:PlayerController;
        public var mount:MountController;
        public var pet:NewPetController;
        public var fashion:FashionController;

        public function PlayerSystemController()
        {
            this.initControllers();
            return;
        }// end function

        private function initControllers() : void
        {
            this.player = new PlayerController();
            this.mount = new MountController();
            this.pet = new NewPetController();
            this.fashion = new FashionController();
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._playerModule == null)
            {
                this._playerModule = new PlayerSystemModule();
                this._playerModule.addEventListener(WindowEvent.SHOW, this.onWinShow);
                this._playerModule.addEventListener(WindowEvent.CLOSE, this.onWinClose);
            }
            return this._playerModule;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.PlayerModuleShowIndexByType, this.openAndShowIndexByType);
            Dispatcher.addEventListener(EventName.PlayerModuleShowIndex, this.openAndShowIndex);
            return;
        }// end function

        private function onWinShow(event:Event) : void
        {
            Dispatcher.addEventListener(EventName.PlayerModuleTabChange, this.winTabChange);
            return;
        }// end function

        private function onWinClose(event:Event) : void
        {
            this.clearContoller();
            Dispatcher.removeEventListener(EventName.PlayerModuleTabChange, this.winTabChange);
            return;
        }// end function

        public function get currentContoller() : PlayerBaseController
        {
            return this._currentContoller;
        }// end function

        private function openAndShowIndex(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (!this.isViewShow)
            {
                this.view.show();
            }
            this._playerModule.changeSelectIndex(_loc_2);
            return;
        }// end function

        private function openAndShowIndexByType(event:DataEvent) : void
        {
            var _loc_3:PlayerTabData = null;
            var _loc_4:int = 0;
            if (!this.isViewShow)
            {
                this.view.show();
            }
            this.updateTabData();
            var _loc_2:* = event.data as String;
            while (_loc_4 < this._dataProvider.length)
            {
                
                _loc_3 = this._dataProvider.getItemAt(_loc_4) as PlayerTabData;
                if (_loc_3.tabType == _loc_2)
                {
                    break;
                }
                _loc_4++;
            }
            this._playerModule.changeSelectIndex(_loc_4);
            return;
        }// end function

        private function updateTabData() : void
        {
            var _loc_2:PlayerTabData = null;
            this._dataProvider = new DataProvider();
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            if (_loc_1 >= 0)
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_Role;
                _loc_2.tabName = Language.getString(35526);
                this._dataProvider.addItem(_loc_2);
            }
            if (GameController.autoGuide.hasGuideMount())
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_Mount;
                _loc_2.tabName = Language.getString(35344);
                this._dataProvider.addItem(_loc_2);
            }
            if (GameController.autoGuide.hasGuidePet())
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_Pet;
                _loc_2.tabName = Language.getString(35345);
                this._dataProvider.addItem(_loc_2);
            }
            if (FashionUtil.isFashionOpend(EFashionType._EFashionWing))
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_Wing;
                _loc_2.tabName = Language.getString(35347);
                _loc_2.panelClass = WingPanel;
                this._dataProvider.addItem(_loc_2);
            }
            if (FashionUtil.isFashionOpend(EFashionType._EFashionClothes))
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_Clothes;
                _loc_2.tabName = Language.getString(35346);
                _loc_2.panelClass = ClothesPanel;
                this._dataProvider.addItem(_loc_2);
            }
            if (FashionUtil.isFashionOpend(EFashionType._EFashionWeapon))
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_Artifact;
                _loc_2.tabName = Language.getString(35348);
                _loc_2.panelClass = ArtifactPanel;
                this._dataProvider.addItem(_loc_2);
            }
            if (_loc_1 >= GameDefConfig.instance.getSystemOpenLevel(69))
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_FightSoul;
                _loc_2.tabName = Language.getString(35349);
                _loc_2.panelClass = FightSoulPanel;
                this._dataProvider.addItem(_loc_2);
            }
            if (_loc_1 >= GameDefConfig.instance.getSystemOpenLevel(70))
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_FightMethod;
                _loc_2.tabName = Language.getString(35350);
                _loc_2.panelClass = FightMethodPanel;
                this._dataProvider.addItem(_loc_2);
            }
            if (_loc_1 >= GameDefConfig.instance.getSystemOpenLevel(71))
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_DragonSoul;
                _loc_2.tabName = Language.getString(35351);
                _loc_2.panelClass = DragonSoulPanel;
                this._dataProvider.addItem(_loc_2);
            }
            if (_loc_1 >= GameConst.HolyBookOpenLevel)
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_HolyBook;
                _loc_2.tabName = Language.getString(35352);
                _loc_2.panelClass = HolyBookPanel;
                this._dataProvider.addItem(_loc_2);
            }
            if (_loc_1 >= GameConst.WarShieldOpenLevel)
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_WarShield;
                _loc_2.tabName = Language.getString(35353);
                _loc_2.panelClass = WarShieldPanel;
                this._dataProvider.addItem(_loc_2);
            }
            if (_loc_1 >= GameConst.MonsterSoulOpenLevel)
            {
                _loc_2 = new PlayerTabData();
                _loc_2.tabType = PlayerConst.TabType_MonsterSoul;
                _loc_2.tabName = Language.getString(35354);
                _loc_2.panelClass = MonsterSoulPanel;
                this._dataProvider.addItem(_loc_2);
            }
            if (this.isViewShow)
            {
                (this.view as PlayerSystemModule).updateTabData(this._dataProvider);
            }
            return;
        }// end function

        private function clearContoller() : void
        {
            if (this._currentContoller)
            {
                this._currentContoller.removeWinShowEvent();
                this._currentContoller.removeWin();
                this._currentContoller = null;
            }
            return;
        }// end function

        private function changeSelectIndex(param1:int) : void
        {
            this._playerModule.changeSelectIndex(param1);
            return;
        }// end function

        private function winTabChange(event:DataEvent) : void
        {
            this.clearContoller();
            var _loc_2:* = event.data as PlayerTabData;
            var _loc_3:* = _loc_2.tabType;
            this._currentContoller = this.getOpenContollerByType(_loc_3);
            if (this._currentContoller)
            {
                if (this._currentContoller is FashionController)
                {
                    (this._currentContoller as FashionController).tabData = _loc_2;
                }
                this._currentContoller.addWinShowEvent();
                this._currentContoller.view.x = _loc_2.posX;
                this._currentContoller.view.y = _loc_2.posY;
                this._playerModule.addChild(this._currentContoller.view);
            }
            return;
        }// end function

        private function getOpenContollerByType(param1:String) : PlayerBaseController
        {
            var _loc_2:PlayerBaseController = null;
            switch(param1)
            {
                case PlayerConst.TabType_Role:
                {
                    _loc_2 = this.player;
                    break;
                }
                case PlayerConst.TabType_Mount:
                {
                    _loc_2 = this.mount;
                    break;
                }
                case PlayerConst.TabType_Pet:
                {
                    _loc_2 = this.pet;
                    break;
                }
                case PlayerConst.TabType_Clothes:
                case PlayerConst.TabType_Wing:
                case PlayerConst.TabType_Artifact:
                case PlayerConst.TabType_FightSoul:
                case PlayerConst.TabType_FightMethod:
                case PlayerConst.TabType_DragonSoul:
                case PlayerConst.TabType_HolyBook:
                case PlayerConst.TabType_WarShield:
                case PlayerConst.TabType_MonsterSoul:
                {
                    _loc_2 = this.fashion;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_2;
        }// end function

    }
}
